$(function () {
    "use strict";
    //加载微博
    $('#stream-item-count').on('click', function () {
        $.ajax({
            url: 'Getmood/getmood',
            type: 'post',
            error: function () {
                $('#stream-item-count').hide();
            },
            success: function (result) {
                var html = '';
                $.each(result.data, function (key, value) {
                    html += '<li class="stream-item" id="' + value.id + '" ><div class="stream-item-detail"><div class="stream-item-content"><div class="stream-item-header"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small></div><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="stream-pic-container"><div class="stream-pic-view">';
                    if (value.photo) {
                        html += '<img src="' + value.photo + '">';
                    }
                    html += '</div></div><div class="stream-item-footer"><div class="stream-item-action-list"><div class="stream-item-action"><button class="stream-item-action-button action-huifu" type="button"><span class="iconfont icon-xiaoxi iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="huifu-count">' + value.hfnum + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-zhuanfa" type="button"><span class="iconfont icon-fenxiang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="zhuanfa-count">' + value.fowardnum + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-dianzan" type="button"><span class="iconfont icon-shoucang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="dianzan-count">' + value.shoucang + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-transhbin" type="button"><span class="iconfont icon-trashbin iconfont-normal"></span></button></div></div></div></div></div><div id="repeat-item" class="stream-item-repeat" style="display: none;"><div class="list-repeat"></div><div class="list-repeats"><ul class="list-box"></ul></div></div></div></li>';
                });

                $('#stream-items-id').html(html);
                $('#stream-item-count').hide();
            }
        });
    });
    $.ajax({
        url: 'Getmood/getmood',
        type: 'post',
        error: function () {

        },
        success: function (result) {
            var html = '';
            $.each(result.data, function (key, value) {
                html += '<li class="stream-item" id="' + value.id + '"><div class="stream-item-detail"><div class="stream-item-content"><div class="stream-item-header"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><a class="timestamp"><span class="_timestamp">' + value.datetime + '</span></a></small></div><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="stream-pic-container"><div class="stream-pic-view">';
                if (value.photo) {
                    html += '<img src="' + value.photo + '">';
                }
                html += '</div></div><div class="stream-item-footer"><div class="stream-item-action-list"><div class="stream-item-action action-huifu"><button class="stream-item-action-button" name="' + value.id + '" type="button"><span class="iconfont icon-xiaoxi iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="huifu-count">' + value.hfnum + '</span></span><input type="hidden" value="1"></button></div><div class="stream-item-action"><button class="stream-item-action-button action-zhuanfa" name="' + value.id + '" type="button"><span class="iconfont icon-fenxiang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="zhuanfa-count">' + value.fowardnum + '</span></span></button></div><div class="stream-item-action"><button class="stream-item-action-button action-dianzan" name="' + value.id + '" type="button"><span class="iconfont icon-shoucang iconfont-normal"></span><span class="icontextcount "><span class="person-count" id="dianzan-count">' + value.shoucang + '</span></span></button></div><div class="stream-item-action action-del"><button class="stream-item-action-button action-transhbin" name="' + value.id + '" type="button"><span class="iconfont icon-trashbin iconfont-normal"></span></button></div></div></div></div></div><div id="repeat-item" class="stream-item-repeat" style="display: none;"><div class="list-repeat"></div><div class="list-repeats"><ul class="list-box"></ul></div></div></div></li>';
            });

            $('#stream-items-id').html(html);
            $('#stream-items-id .action-huifu').delegate('button', 'click', function (event) {
                event.stopPropagation();
                event.preventDefault();

                var flag = $(this).find(':input:eq(0)');
                var t = $(this).parent().parent().parent().parent().parent().parent();
                if (flag.val() == 1) {
                    flag.attr('value', '0');
                    t.find('.stream-item-repeat').show();
                    var s = $('.me-btn img')[0].src;
                    var h = t.find('.username').find('b').text();
                    /*repeat 头*/
                    var html = '<div class="repeat"><div class="repeat-avatar size32"><img class="size32" src="' + s + '"></div><div class="publish"><div class="p-input"><textarea class="repeat-textarea" name="content" style="margin: 0;border-style: none;border-width: 0;font-size: 14px;word-wrap:break-word;line-height: 18px;overflow-y: auto;overflow-x: hidden;outline: none;"></textarea></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn" class="send-box-btn send-repeat" type="button" disabled="disabled">评论</button></div></div></div><div class="repeat-list"></div></div>';
                    t.find(".list-repeat").html(html);
                    /*send repeat*/
                    //判断文本框字数
                    t.find(".repeat-textarea").on("input", function () {
                        var hftext = $(this).val();
                        var s = new RegExp(/^\s+$/);
                        var num = $(this).val().length;
                        if (num === 0 || num > 140) {
                            t.find("#repeat-btn").attr("disabled", "disabled");
                        } else {
                            if (s.test(hftext)) {
                                t.find("#repeat-btn").attr("disabled", "disabled");
                            } else {
                                t.find("#repeat-btn").removeAttr("disabled");
                            }
                        }
                    });
                    t.find("#repeat-btn").on('click', function () {
                        var content = t.find(".repeat-textarea").val();
                        var tocomment = t.attr('id');
                        var ajaxflag = true;
                        if (ajaxflag) {
                            ajaxflag = false;
                            $.ajax({
                                url: 'Repeat/sendrepeat',
                                type: 'post',
                                data: {
                                    content: content,
                                    tocomment: tocomment,
                                    towho: h
                                },
                                success: function (result) {
                                    t.find(".action-huifu #huifu-count").html(result);
                                    t.find(".repeat-textarea").val("");
                                    $.ajax({
                                        url: 'Repeat/getrepeat',
                                        type: 'post',
                                        data: {
                                            id: t.attr('id')
                                        },
                                        success: function (result) {
                                            var html = '';
                                            var hfnum = '';
                                            $.each(result.data, function (key, value) {
                                                hfnum = value.hfnum;
                                                html += '<li class="list-li" id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn repeat-btn2" type="button">回复</button></div></div></div></li>';
                                            });
                                            t.find('.list-box').html(html);
                                            t.find(".action-huifu #huifu-count").html(hfnum);
                                            t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                                            t.find('.list-box li').last().css("border", "none");
                                            sendrepeat();
                                        }
                                    });
                                    t.find("#repeat-btn").attr("disabled", "disabled");
                                }
                            });
                        } else {
                            setTimeout(function () {
                                ajaxflag = true;
                            }, 1000);
                        }
                    });
                    $.ajax({
                        url: 'Repeat/getrepeat',
                        type: 'post',
                        data: {
                            id: t.attr('id')
                        },
                        success: function (result) {
                            var html = '';
                            var hfnum = '';
                            $.each(result.data, function (key, value) {
                                hfnum = value.hfnum;
                                html += '<li class="list-li" id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn repeat-btn2" type="button">回复</button></div></div></div></li>';
                            });
                            t.find('.list-box').html(html);
                            t.find(".action-huifu #huifu-count").html(hfnum);
                            t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                            t.find('.list-box li').last().css("border", "none");
                            sendrepeat();
                        }
                    });

                } else {
                    flag.attr('value', '1');
                    t.find('.stream-item-repeat').hide();
                }

                function sendrepeat() {
                    t.find(".repeat-btn2").off().on('click', function () {
                        var th = $(this).parent().parent().parent();
                        var thid = th.attr('id');
                        $('#global-repeat-box').find('.modal-title').empty();
                        $("#global-repeat-box .send-box-textarea").val('');
                        $('.modal-repeat').empty();
                        $.ajax({
                            url: 'Repeat/getwhorepeat',
                            type: 'post',
                            data: {
                                towho: thid
                            }, success: function (result) {
                                var html = '';
                                var h3 = '';
                                $.each(result.data, function (key, value) {
                                    html = '<div class="list-li" id="' + value.who + '"><a class="account-group" href="' + value.username + '"><img class="stream-item-avater size48" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div>';
                                    h3 = '回复@' + value.username + '';
                                });
                                $('#global-repeat-box').find('.modal-title').html(h3);
                                h3 += '：';
                                $('#global-repeat-box').find('.send-box-textarea').html(h3);
                                $('.modal-repeat').html(html);
                            }
                        });
                        $('#global-repeat-box').css("display", "block");
                        $('.global-dialog').show();

                        $("#global-repeat-box .send-box-textarea").on("input", function () {
                            var towho = $('#global-repeat-box').find('.modal-title').text();
                            towho += '：';
                            var hftext = $(this).val();
                            var s = new RegExp(/^\s+$/);
                            var num = $(this).val().length;
                            $('#global-repeat-box .counter').html(140 - num);

                            if (num === 0 || num > 140) {
                                $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                            } else {
                                if (s.test(hftext)) {
                                    $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");

                                } else if (towho == hftext) {
                                    $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                                } else {
                                    $("#global-repeat-box .send-box-btn").removeAttr("disabled");
                                }
                            }
                        });
                        $("#global-repeat-box .send-repeat").off().on('click', function () {
                            $("#global-repeat-box .send-box-btn").attr("disabled", "disabled");
                            var content = $('#global-repeat-box .send-box-textarea').val();
                            var tocomment = t.attr('id');
                            var towho = $('#global-repeat-box').find('.username').find('b').text();
                            $.ajax({
                                url: 'Repeat/sendrepeat',
                                type: 'post',
                                data: {
                                    content: content,
                                    tocomment: tocomment,
                                    towho: towho
                                },
                                success: function (result) {
                                    t.find(".action-huifu #huifu-count").html(result);
                                    t.find(".repeat-textarea").val("");
                                    $('#global-repeat-box .send-box-textarea').val("");
                                    $.ajax({
                                        url: 'Repeat/getrepeat',
                                        type: 'post',
                                        data: {
                                            id: t.attr('id')
                                        },
                                        success: function (result) {
                                            var html = '';
                                            var hfnum = '';
                                            $.each(result.data, function (key, value) {
                                                hfnum = value.hfnum;
                                                html += '<li class="list-li"  id="' + value.id + '"><a id="' + value.who + '" class="account-group" href="' + value.username + '"><img class="stream-item-avater size32" src="' + value.avatar + '"><strong class="fullname">' + value.nickname + '</strong><span>&rlm;</span><span class="username">@<b>' + value.username + '</b></span></a><small class="time"><span class="timestamp"><span class="_timestamp">' + value.datetime + '</span></span></span></small><div class="stream-item-text-container"><p class="stream-item-text">' + value.content + '</p></div><div class="p-send"><div class="send-box-bar"><button id="repeat-btn2" class="send-box-btn" type="button">回复</button></div></div></div><div class="repeat-list"></div></li>';
                                            });
                                            t.find('.list-box').html(html);

                                            t.find('.list-box li').first().css("border-top", "1px solid #d9d9d9");
                                            t.find('.list-box li').last().css("border", "none");
                                        }
                                    });
                                }
                            });
                            $('#global-repeat-box').css("display", "none");
                            $(".global-dialog").hide();
                        });
                    });
                }
            });

            $('#stream-items-id .action-del').delegate('button', 'click', function (event) {
                event.stopPropagation();
                event.preventDefault();

                var t = $(this).parent().parent().parent().parent().parent().parent();
                var h = t.find('.username').find('b').text();
            });
        }
    });
    //加载推送 30s发送一次
    function getcount() {
        var moodid = $('ol li:first-child').attr('id');
        $.ajax({
            url: 'Getmood/getcount',
            type: 'post',
            data: {
                id: moodid
            },
            error: function () {
                return;
            },
            success: function (result) {
                if (result != 0 && result != undefined) {
                    $('#stream-item-count').show();
                    var html = '';
                    html += '<div class="new-weblog" id="new_weblog">你有' + result + '条新的微博</div>';
                    $('#stream-item-count').html(html);

                }
                return;
            }
        });
    }

    /* 第一次获取通知数目*/
    setTimeout(function () {
        getcount();
    }, 8000);
    /* 30s轮询*/
    setInterval(function () {
        getcount();
    }, 5000);//无意义的发送有点多




});